package graphique;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import executer.Controleur;


public class Connection extends JFrame{
	
	private static final long serialVersionUID = 1L;
	private int longueurFen= 350;
    private int largeurFen = 300;
	//private boolean sendData;
	private JLabel pwdLabel, idLabel, newLabel/*, titleLabel, tailleLabel, taille2Label, icon*/;
	//private JRadioButton tranche1, tranche2, tranche3, tranche4;
	//private JComboBox sexe, cheveux;
	private JTextField jtfid = new JTextField("");
	private JTextField jtfnew = new JTextField("");	
	private JPasswordField jpfpwd = new JPasswordField("");
	private JButton boutonLog = new JButton("OK");
	private JButton boutonNew = new JButton("OK");
	public Fenetre f;
	private Controleur control;
	//public Panneau pan = new Panneau();
	boolean identificationCorrecte = true;
	
	
	public boolean getIdentificationCorrecte(){
		return identificationCorrecte;
	}
	
	public void setIdentificationCorrecte(boolean id){
		identificationCorrecte = id;
	}

	public Connection(Controleur control){
		super();
		this.control = control;
		//titre de la fenetre
		setTitle("Agenda Electronique");
		//taille de la fenetre
        setSize(largeurFen,longueurFen);
        //place la fenetre au centre de l'ecran
        setLocationRelativeTo(null);
        //termine le processus lorsque l'on clic sur la croix
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setLayout(new BorderLayout());

		//cadre identification
		
		JPanel panLog = new JPanel();
		
		panLog.setBackground(Color.white);
		panLog.setPreferredSize(new Dimension(220, 130)); //dimension du cadre
		
		panLog.setBorder(BorderFactory.createTitledBorder("Identification"));
		jtfid.setPreferredSize(new Dimension(100, 25));
        jtfid.setForeground(Color.black);
		Font police = new Font("Arial", Font.BOLD, 12);
        jtfid.setFont(police);
		idLabel = new JLabel("Identifiant :");
		panLog.add(idLabel);
		panLog.add(jtfid);
		
		jpfpwd.setPreferredSize(new Dimension(100, 25));
		pwdLabel =new JLabel("Password :");
		panLog.add(pwdLabel);
		panLog.add(jpfpwd);
		
		panLog.add(boutonLog);
		
		
		boutonLog.addActionListener(new ActionListener(){
    		@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent event){
    			String login = jtfid.getText();
    			String pass = jpfpwd.getText();
    			if(Connection.this.control.verifyLogger(login, pass)) {
    				dispose();
    				Connection.this.control.openFile(login);
    				f = new Fenetre(Connection.this.control);
                    f.setTitle("Agenda electronique de "+login );
    			} else {
    				boiteIdErreur();
    			}
    		}
    	});

		
		//cadre creation compte
		
		JPanel panNewUser = new JPanel();
		
		panNewUser.setBackground(Color.white);
		panNewUser.setPreferredSize(new Dimension(220, 130)); //dimension du cadre
		
		panNewUser.setBorder(BorderFactory.createTitledBorder("Nouvel Utilisateur"));
        jtfnew.setForeground(Color.black);
        Font police1 = new Font("Arial", Font.BOLD, 12);
        jtfnew.setFont(police1);
		jtfnew.setPreferredSize(new Dimension(100, 25));
		newLabel = new JLabel("Veuillez saisir un Identifiant :");
		panNewUser.add(newLabel);
		panNewUser.add(jtfnew);
		
		panNewUser.add(boutonNew);
		
		boutonNew.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				String str = jtfnew.getText();
				if(Connection.this.control.verifyLoggin(str)) {
					new FormulaireCompte("Creation compte utilisateur", Connection.this.control, str);
				} else {
					boiteIdIndispo();
				}
			}
			
		});
		
		
		
		JPanel content1 = new JPanel();
		JPanel content2 = new JPanel();
		content1.setBackground(Color.white);
		content2.setBackground(Color.white);
		content1.add(panLog);
		content2.add(panNewUser);
		add(content1, BorderLayout.NORTH);
		add(content2, BorderLayout.CENTER);
		setVisible(true);
		
	}
	
	private void boiteIdIndispo()
    {
        JOptionPane.showMessageDialog(null, 
                                      "Desoler mais cet identifiant n'est pas disponible",
                                      "Erreur", 
                                      JOptionPane.ERROR_MESSAGE);
    }

	private void boiteIdErreur()
    {
        JOptionPane.showMessageDialog(null, 
                                      "Erreur d'identifiant/mot de passe",
                                      "Erreur", 
                                      JOptionPane.ERROR_MESSAGE);
    }
	
	
	
}
